﻿<?php if (!defined('SERVER_ROOT')) exit('No direct script access allowed');

class bckh_Model extends Model {

    function __construct()
    {
        parent::__construct();
    }
    public function qry_all_bckh()
    {
        return $this->db->ExecuteCursor("BEGIN CONGTAC2.SELECT_BCKH(:CUR); END;",'CUR');
    }
    public function qry_single_bckh($TEN_BCKH)
    {
       $P_MA_CAN_BO = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
       return $this->db->ExecuteCursor("BEGIN CONGTAC2.SELECT_BCKH_DETAIL(:CUR,$P_MA_CAN_BO,'$TEN_BCKH'); END;",'CUR');
    }
    public function update_bckh()
    {
        $P_UPDATE_TYPE = get_post_var('hdn_update_type');
        $P_NGUOI_CAP_NHAT = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_CAN_BO = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_TEN_HOC_KY = get_post_var('txt_TEN_HOC_KY');
        $P_MA_LOAI_BCKH = get_post_var('cbx_MA_LOAI_BCKH');
        $P_TEN_BCKH = get_post_var('txt_TEN_BCKH');
        $P_SO_TAP_CHI = get_post_var('txt_SO_TAP_CHI');
        $P_VI_TRI_TRANG = get_post_var('txt_VI_TRI_TRANG');
        $P_TEN_NHA_XUAT_BAN = get_post_var('txt_TEN_NHA_XUAT_BAN');
        $P_NOI_LUU_TRU = get_post_var('txt_NOI_LUU_TRU');
        $P_THOI_GIAN_DANG = get_post_var('txt_THOI_GIAN_DANG');

        $stmt = $this->db->PrepareSP("BEGIN CONGTAC2.UPDATE_BCKH(:P_VAL,
                                                                :P_UPDATE_TYPE,
                                                                :P_NGUOI_CAP_NHAT,
                                                                :P_MA_CAN_BO,
                                                                :P_TEN_HOC_KY,
                                                                :P_MA_LOAI_BCKH,
                                                                :P_TEN_BCKH,
                                                                :P_SO_TAP_CHI,
                                                                :P_VI_TRI_TRANG,
                                                                :P_TEN_NHA_XUAT_BAN,
                                                                :P_NOI_LUU_TRU,
                                                                :P_THOI_GIAN_DANG
                                                                );END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_UPDATE_TYPE,'P_UPDATE_TYPE');
        $this->db->InParameter($stmt,$P_NGUOI_CAP_NHAT,'P_NGUOI_CAP_NHAT');
        $this->db->InParameter($stmt,$P_MA_CAN_BO,'P_MA_CAN_BO');
        $this->db->InParameter($stmt,$P_TEN_HOC_KY,'P_TEN_HOC_KY');
        $this->db->InParameter($stmt,$P_MA_LOAI_BCKH,'P_MA_LOAI_BCKH');
        $this->db->InParameter($stmt,$P_TEN_BCKH,'P_TEN_BCKH');
        $this->db->InParameter($stmt,$P_SO_TAP_CHI,'P_SO_TAP_CHI');
        $this->db->InParameter($stmt,$P_VI_TRI_TRANG,'P_VI_TRI_TRANG');
        $this->db->InParameter($stmt,$P_TEN_NHA_XUAT_BAN,'P_TEN_NHA_XUAT_BAN');
        $this->db->InParameter($stmt,$P_NOI_LUU_TRU,'P_NOI_LUU_TRU');
        $this->db->InParameter($stmt,$P_THOI_GIAN_DANG,'P_THOI_GIAN_DANG');
        $chaythutuc = $this->db->Execute($stmt);
        //echo '<script>alert("'.$P_VAL.'");</script>';
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->popup_exec_done();
          }elseif($P_VAL == 'TON_TAI_BCKH'){
              $this->popup_exec_fail(__('Tên BCKH đã tồn tại!'));
              return;
          }else{
               $this->popup_exec_fail(__('Không thể cập nhật!'));
               return;
          }
        }
    }
    public function delete_bckh(){
        $P_NGUOI_XOA = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_TEN_BCKH = get_post_var('hdn_item_id',0);
        //echo '<script>alert("'.$P_MA_BCKH.'");</script>';
        $stmt = $this->db->PrepareSP("BEGIN CONGTAC2.DELETE_BCKH(:P_VAL,:P_NGUOI_XOA,:P_TEN_BCKH);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_NGUOI_XOA,'P_NGUOI_XOA');
        $this->db->InParameter($stmt,$P_TEN_BCKH,'P_TEN_BCKH');
        $chaythutuc = $this->db->Execute($stmt);
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->exec_done($this->goback_url);
          }else{
               $this->popup_exec_fail(__('Không thể xóa!'));
               return;
          }
        }
    }
}